/**
 * 
 */
package com.exam.base.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.exam.role.model.User;
import com.exam.util.Constants;
import com.exam.util.Pagetools;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author huang.hj
 * 
 */
public class BaseAction extends ActionSupport {
	/**
	 * 系统运行日志.
	 */
	protected final Logger logger = Logger.getLogger(getClass());
	protected final static String REQUEST = "1";
	protected final static String SESSION = "2";

	/**
	 * Convenience method to get the request.
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response.
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session.
	 * 
	 * @return session
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 
	 * @param scope:作用域
	 * @param attribute:属性
	 */
	protected void setAttribute(String scope, String attribute, Object obj) {
		if (REQUEST.equals(scope)) {
			getRequest().setAttribute(attribute, obj);
		} else if (SESSION.equals(scope)) {
			getSession().setAttribute(attribute, obj);
		}
	}

	protected User getCurrentLoginUser() {
		return Constants.loginUser(getSession());
	}
	/**
	 * 分页对象
	 * @return
	 */
	protected Pagetools getPage() {
		Pagetools page = new Pagetools();
		HttpServletRequest request = getRequest();
		String offset = request.getParameter("offset");
		String pageSize = request.getParameter("pageSize");
		if (!StringUtils.isEmpty(offset)) {
			page.setOffset(Integer.parseInt(offset));
		}
		if (!StringUtils.isEmpty(pageSize)) {
			page.setOffset(Integer.parseInt(pageSize));
		}
		return page;
	}

}
